package com.example.springmvcproject.Service;

import cn.dev33.satoken.stp.StpUtil;
import com.example.springmvcproject.Mapper.CreditMapper;
import com.example.springmvcproject.Pojo.Credit;
import com.example.springmvcproject.Service.Inteface.CreditService;
import com.example.springmvcproject.Utils.ResultCode;
import com.example.springmvcproject.Utils.ResultMessage;
import com.example.springmvcproject.Utils.SaTokenUtils;
import com.example.springmvcproject.Vo.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CreditServiceImpl implements CreditService {
    @Resource
    CreditMapper creditMapper;


    /**
     * 添加一个信用卡
     *
     * @param creditVo
     * @return
     */
    @Override
    public ResultMessage addCredit(CreditVo creditVo) {
        //获取登陆的ID
        Integer userId = StpUtil.getLoginIdAsInt();
        creditMapper.addCredit(new  Credit(null,
                creditVo.getCreditNum(),
                creditVo.getBankId(),
                new Date().getTime(),
                creditVo.getRepayTime(),
                creditVo.getRepayAmount(),
                0,
                creditVo.getRemarks(),
                userId));
        return new ResultMessage(ResultCode.RESULT_OK.getCode(), ResultCode.RESULT_OK.getStatus(), null,"信用卡添加成功");
    }

    @Override
    public ResultMessage selectCreditsBySelective(CreditSelectiveVo creditSelectiveVo) {
        //获取用户登录的ID
        Integer userId=StpUtil.getLoginIdAsInt();
        Page<Object> objects = PageHelper.startPage(creditSelectiveVo.getPageIndex(), creditSelectiveVo.getPageSize());
        String columnTimeName=null;
        //判断查询时间的类型字段并重新赋值
        String timeType = creditSelectiveVo.getTimeType();
        Map<String,String> timeMap=new HashMap<>();
        timeMap.put("createTime","credit_createTime");
        timeMap.put("repayTime","credit_repayTime");
        if (timeType!=null&&timeMap.get(timeType)!=null){
            columnTimeName=timeMap.get(timeType);
        }
        //获取类型查询
        String columnSearchType=null;
        String searchType=creditSelectiveVo.getSearchType();
        Map<String,String> searchMap=new HashMap<>();
        searchMap.put("num","credit_num");
        searchMap.put("remarks","create_remarks");
        if (searchType!=null&&searchMap.get(searchType)!=null){
            columnSearchType=searchMap.get(creditSelectiveVo.getSearchType());
        }
        List<Credit> data=creditMapper.selectCreditsBySelective(
                columnTimeName,
                creditSelectiveVo.getStartTime(),
                creditSelectiveVo.getEndTime(),
                columnSearchType,
                creditSelectiveVo.getSearchText(),
                userId,
                creditSelectiveVo.getStatus()
                );
        return new ResultMessage(ResultCode.RESULT_OK.getCode(), ResultCode.RESULT_OK.getStatus(), null,new PageResult(objects.getTotal(),objects.getPageNum(),objects.getPageSize(),data));
    }

    @Override
    public ResultMessage deleteCreditById(CreditDelVo creditDelVo) {
        //获取登录userId
        Integer userId=StpUtil.getLoginIdAsInt();
        //执行sql
        Credit credit = new Credit();
        credit.setCreditId(creditDelVo.getCreditId());
        credit.setCreditNum(creditDelVo.getCreditNum());
        credit.setUserId(userId);
        boolean flag=creditMapper.deleteCreditById(credit);
        if (!flag) return  new ResultMessage(ResultCode.RESULT_NULL.getCode(), ResultCode.RESULT_NULL.getStatus(), "非法传参",null);
        return new ResultMessage(ResultCode.RESULT_OK.getCode(), ResultCode.RESULT_OK.getStatus(), null,"删除成功");
    }

    /**
     * 修改还款状态，根据ID和卡号
     *
     * @param creditUpVo
     * @return
     */
    @Override
    public ResultMessage updateStatusByIdAndNum(CreditUpVo creditUpVo) {
        //获取登录userId
        Integer userId=StpUtil.getLoginIdAsInt();
        //执行sql
        Credit credit = new Credit();
        credit.setCreditId(creditUpVo.getCreditId());
        credit.setCreditNum(creditUpVo.getCreditNum());
        credit.setCreditStatus(creditUpVo.getCreditStatus());
        credit.setUserId(userId);
        boolean flag=creditMapper.updateStatusByIdAndNum(credit);
        if (!flag) return  new ResultMessage(ResultCode.RESULT_NULL.getCode(), ResultCode.RESULT_NULL.getStatus(), "非法传参",null);
        return new ResultMessage(ResultCode.RESULT_OK.getCode(), ResultCode.RESULT_OK.getStatus(), null,"修改成功");

    }
}
